//
// *  $Id: TestRecordFile.java,v 1.4 2001/11/10 18:36:50 boisvert Exp $
// *
// *  Unit tests for RecordFile class
// *
// *  Simple db toolkit
// *  Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
// *
// *  This library is free software; you can redistribute it and/or
// *  modify it under the terms of the GNU Library General Public License
// *  as published by the Free Software Foundation; either version 2
// *  of the License, or (at your option) any later version.
// *
// *  This library is distributed in the hope that it will be useful,
// *  but WITHOUT ANY WARRANTY; without even the implied warranty of
// *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// *  Library General Public License for more details.
// *
// *  You should have received a copy of the GNU Library General Public License
// *  along with this library; if not, write to the Free Software Foundation,
// *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
// 

//*************************************************************************
//**  Included in JDMB 1.0 port to SharpDBM;  11/2013 Cyrus Neah cneah@codingreal.com
//**  SharpDBM is an independent reimplementation of the JDBM 1.0 software library  
//*************************************************************************

using System;
using SharpDBM;
using SharpDBM.recman;
using SharpDBM.helper;
using SharpDBM.btree;
using NUnit.Framework;



namespace SharpDBM.Test

.recman
{

	using NUnit.Framework;
    using System.IO;

///
// *  This class contains all Unit tests for {@link RecordFile}.
// 
	[TestFixture]
    public class TestRecordFile  
	{

		public const string testFileName = "test";

	

		public static void deleteFile(string filename)
		{

            

            FileStream file = new FileStream(filename,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite);
            FileInfo fi = new FileInfo(filename);
            
			if (fi.Exists)
			{
				try
				{
                    file.Close();
                    fi.Delete();
                    fi = new FileInfo(filename);
				}
				catch (Exception except)
				{
					Console.WriteLine(except.StackTrace);
				}
                if (fi.Exists)
				{
					Console.WriteLine("WARNING:  Cannot delete file: " + file);
				}
			}
		}


		public static void deleteTestFile()
		{
			//System.gc();
            GC.Collect(); 
            GC.SuppressFinalize(testFileName);

			deleteFile(testFileName);

			deleteFile(testFileName + RecordFile.extension);

			deleteFile(testFileName + TransactionManager.extension);
		}

		[Test]
 public virtual void setUp()
		{
			deleteTestFile();
		}

		[Test]
 public virtual void tearDown()
		{
			deleteTestFile();
		}


//    *
//     *  Test constructor
//     
 
//
		[Test]
 public virtual void testCtor()
		{
			RecordFile file = new RecordFile(testFileName);
			file.close();
		}


//    *
//     *  Test addition of record 0
//     
 
//
		[Test]
 public virtual void testAddZero()
		{
			RecordFile file = new RecordFile(testFileName);
			sbyte[] data = file.get(0).getData();
			data[14] = (sbyte) 'b';
			file.release(0, true);
			file.close();
			file = new RecordFile(testFileName);
			data = file.get(0).getData();
			NUnit.Framework.Assert.AreEqual((sbyte) 'b', data[14]);
			file.release(0, false);
			file.close();
		}


//    *
//     *  Test addition of a number of records, with holes.
//     
 
//
		[Test]
 public virtual void testWithHoles()
		{
			RecordFile file = new RecordFile(testFileName);

		// Write recid 0, byte 0 with 'b'
			sbyte[] data = file.get(0).getData();
			data[0] = (sbyte) 'b';
			file.release(0, true);

		// Write recid 10, byte 10 with 'c'
			data = file.get(10).getData();
			data[10] = (sbyte) 'c';
			file.release(10, true);

		// Write recid 5, byte 5 with 'e' but don't mark as dirty
			data = file.get(5).getData();
			data[5] = (sbyte) 'e';
			file.release(5, false);

			file.close();

			file = new RecordFile(testFileName);
			data = file.get(0).getData();
            NUnit.Framework.Assert.AreEqual((sbyte)'b', data[0], "0 = b");
			file.release(0, false);

			data = file.get(5).getData();
            NUnit.Framework.Assert.AreEqual(0, data[5], "5 = 0");
			file.release(5, false);

			data = file.get(10).getData();
            NUnit.Framework.Assert.AreEqual((sbyte)'c', data[10], "10 = c");
			file.release(10, false);

			file.close();
		}


//    *
//     *  Test wrong release
//     
 
//
		[Test]
 public virtual void testWrongRelease()
		{
			RecordFile file = new RecordFile(testFileName);

		// Write recid 0, byte 0 with 'b'
			sbyte[] data = file.get(0).getData();
			data[0] = (sbyte) 'b';
			try
			{
				file.release(1, true);
				NUnit.Framework.Assert.Fail("expected exception");
			}
			catch (IOException except)
			{
			// ignore
			}
			file.release(0, false);

			file.close();

		// @alex retry to open the file
//        
//        file = new RecordFile( testFileName );
//        PageManager pm = new PageManager( file );
//        pm.close();
//        file.close();
//        
		}


//    *
//     *  Runs all tests in this class
//     
		static void Main(string[] args)
		{
			// junit.textui.TestRunner.run(new TestSuite(typeof(TestRecordFile)));
		}
	}

}